﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using StyleMVVM.Utilities;
#if NETFX_CORE
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using StyleMVVM.Utilities;

#endif

namespace StyleMVVM.View
{
	public sealed class DesignTime
	{
		public static string GetBackground(DependencyObject obj)
		{
			return (string)obj.GetValue(InternalBackgroundProperty);
		}

		public static void SetBackground(DependencyObject obj, string value)
		{
			obj.SetValue(InternalBackgroundProperty, value);
		}

		public static DependencyProperty BackgroundProperty
		{
			get { return InternalBackgroundProperty; }
		}

		// Using a DependencyProperty as the backing store for Background.  This enables animation, styling, binding, etc...
		internal static readonly DependencyProperty InternalBackgroundProperty =
			DependencyProperty.RegisterAttached("Background",
			                                    typeof(string),
			                                    typeof(DesignTime),
			                                    new PropertyMetadata(null, BackgroundChanged));

		private static void BackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			if (DesignMode.IsEnabled)
			{
				if (d is Panel)
				{
					Panel control = d as Panel;

					if (e.NewValue is string)
					{
						control.Background = FindBrush(control, e.NewValue as string);
					}
					else
					{
						control.Background = null;
					}
				}
				else if (d is Control)
				{
					Control control = d as Control;

					if (e.NewValue is string)
					{
						control.Background = FindBrush(control, e.NewValue as string);
					}
					else
					{
						control.Background = null;
					}
				}
				else if (d is Border)
				{
					Border border = d as Border;

					if (e.NewValue is string)
					{
						border.Background = FindBrush(border, e.NewValue as string);
					}
					else
					{
						border.Background = null;
					}
				}
			}
		}

		private static Brush FindBrush(DependencyObject d, string brushName)
		{
			Brush returnValue = null;

			try
			{
				returnValue = Application.Current.Resources[brushName] as Brush;
			}
			catch (Exception)
			{
			}

			if (returnValue == null && d is FrameworkElement)
			{
				FrameworkElement element = d as FrameworkElement;

				try
				{
					returnValue = element.Resources[brushName] as Brush;
				}
				catch (Exception)
				{
				}
			}

			if (returnValue == null)
			{
				try
				{
					PropertyInfo propertyInfo = typeof(Colors).GetTypeInfo().GetDeclaredProperty(brushName);

					if (propertyInfo != null)
					{
						returnValue = new SolidColorBrush((Color)propertyInfo.GetValue(null));
					}
				}
				catch (Exception)
				{
				}
			}

			return returnValue;
		}
	}
}